package com.kehutong.repair.utils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.coraframework.util.Objects;

import com.kehutong.repair.entity.Repairs;
import com.kehutong.repair.entity.RepairsType;
import com.kehutong.repair.enums.RepairsStatus;

public class RepairsUtils {

    /**
     * 获取响应倒计时
     * @param repairs
     * @return
     */
    public static String getCountdown(Repairs repairs) {
        StringBuilder countdown = new StringBuilder();
        // 超时时间
        LocalDateTime timeOut = repairs.getTimeOut();
        if(repairs.getRepairsStatus() == RepairsStatus.CANCEL){
            return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(repairs.getTimeOut());
        }
        // 超时
        if (repairs.getRepairsStatus() == RepairsStatus.PENDING || repairs.getRepairsStatus() == RepairsStatus.PROCESSING) {
            if (LocalDateTime.now().isAfter(timeOut)) {
                countdown.append("已超时");
            }
        }else {
            if (repairs.getCompleteTime().isAfter(timeOut)) {
                countdown.append("已超时");
            }
        }
        //已处理
        if (Objects.nonNull(repairs.getCompleteTime())) {
            String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(repairs.getCompleteTime());
            countdown.append("完成 " + format);
            return countdown.toString();

        }
        //未完成
        Duration duration = Duration.between(LocalDateTime.now(), timeOut);
        long days = Math.abs(duration.toDays());      //相差的天数
        long hours = Math.abs(duration.toHours()%24);    //相差的小时数
        long minutes = Math.abs(duration.toMinutes());
        if (Objects.nonNull(minutes)) {
            hours++;
        }
        countdown.append(days + "天" + hours + "时");
        return countdown.toString();
    }


    /**
     * 拼接限时
     * @return
     */
    public static String jointTime(RepairsType repairsType){
        return repairsType.getTime() + repairsType.getUnit().getDesc();
    }
}
